package org.example.chatPage;

import org.apache.xpath.operations.Or;
import org.junit.Before;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;

import java.util.List;

public class ChatPageTest {
    public static WebDriver webDriver;

    @BeforeAll
    public static void before() throws InterruptedException {
        webDriver = new ChromeDriver();
        webDriver.get("http://123.60.88.108:8080/login.html");
        Thread.sleep(2000);
        //输入用户名和密码
        WebElement element1 = webDriver.findElement(By.cssSelector("#username"));
        WebElement element2 = webDriver.findElement(By.cssSelector("#password"));
        //情况输入框
        element1.clear();
        element2.clear();

        //输入用户名
        element1.sendKeys("zhangsan");

        //输入密码
        element2.sendKeys("123456");

        //点击登录
        WebElement element3 = webDriver.findElement(By.cssSelector("#submit"));
        element3.click();
        Thread.sleep(2000);
    }

    //检查会话按钮元素是否存在
    @Order(1)
    @Test
    public void test1() {
        WebElement sessionButton = webDriver.findElement(By.className("tab-session"));
        if (sessionButton.isDisplayed()) {
            System.out.println("测试成功：会话按钮元素存在");
        }
    }

    //检查好友列表按钮是否存在
    @Order(2)
    @Test
    public void test2() {
        WebElement friendListButton = webDriver.findElement(By.className("tab-friend"));
        if (friendListButton.isDisplayed()) {
            System.out.println("测试成功：好友列表按钮存在");
        }
    }

    //检查当前登录用户名是否正确显示
    @Order(3)
    @Test
    public void test3() {
        WebElement element = webDriver.findElement(By.className("user"));
        if (element.isDisplayed()) {
            System.out.println("测试成功：用户名正确显示{" + element.getText() + "}");
        }
    }

    //检查好友列表能否正常显示
    public void test4() throws InterruptedException {
        //点击好友列表按钮
        WebElement friendButton = webDriver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.table > div.tab-friend"));
        friendButton.click();
        WebElement element = webDriver.findElement(By.xpath("//*[@id=\"friend-list\"]"));
        List<WebElement> elements = element.findElements(By.tagName("li"));

        for (WebElement e : elements) {
            if (e.isDisplayed()) {
                System.out.println("好友：" + e.getText() + ", 测试成功");
            } else {
                System.out.println("测试失败");
            }
        }
    }

    //检查会话列表是否正常显示
    @Order(4)
    @Test
    public void test5(){
        WebElement element = webDriver.findElement(By.cssSelector("#session-list"));
        List<WebElement> elements = element.findElements(By.tagName("li"));
        for(WebElement e : elements) {
            if(e.isDisplayed()) {
                System.out.println("会话：" + e.getText() + ",测试成功");
            }else {
                System.out.println("测试失败");
            }
        }
    }
    //点击会话后，是否正确显示聊天对话开框及历史消息
    @Order(5)
    @Test
    public void Test6() {
        WebElement session = webDriver.findElement(By.cssSelector("#session-list > li:nth-child(2)"));
        //获取会话中显示的好友名称
        WebElement sessionNameElement = session.findElement(By.tagName("h3"));
        String sessionName = sessionNameElement.getText();
        session.click();
        //获取聊天上好友用户名
        WebElement friendNameElement = webDriver.findElement(By.cssSelector("body > div.client-container > div > div.right > div.title"));
        String friendName = friendNameElement.getText();
        if(!sessionName.equals(friendName)) {
            System.out.println("测试失败，好友名字不匹配");
        }
        //检查聊天消息是否能够正常显示
        WebElement element = webDriver.findElement(By.xpath("/html/body/div[2]/div/div[2]/div[2]"));
        List<WebElement> elements = element.findElements(By.className("box"));
        System.out.println(elements.size());
        for(WebElement e : elements) {
            WebElement message = e.findElement(By.tagName("p"));
            if(message.isDisplayed()) {
                System.out.println("历史消息：" + message.getText());
            }
        }

    }

}

